package com.yc.bdw.Activity;

import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yc.bdw.Adapter.MyPagerAdapter;
import com.yc.bdw.Adapter.PhoneShareListAdapter;
import com.yc.bdw.R;
import com.yc.bdw.Utils.ListUtils;
import com.yc.bdw.model.Product;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.BindView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class PhoneShareActivity extends KJActivity implements GestureDetector.OnGestureListener {
    @BindView(id = R.id.phoneshare_back, click = true)
    private ImageView backBtn;
    @BindView(id = R.id.phoneshare_advertisement, click = true)
    private ImageView advertisementImg;
    @BindView(id = R.id.phoneshare_item1, click = true)
    private TextView item1Text;
    @BindView(id = R.id.phoneshare_item2, click = true)
    private TextView item2Text;
    @BindView(id = R.id.phoneshare_slidebar)
    private View slidebar;
    @BindView(id = R.id.phoneshare_commodities, click = true)
    private ListView commoditiesList;
    @BindView(id = R.id.viewPager, click = true)
    private ViewPager viewPager;

    private List<Product> products = new ArrayList<Product>();
    private PhoneShareListAdapter adapter;

    private int windowsWidth;
    private GestureDetector gestureDetector;
    private final int item1 = 0;
    private final int item2 = 1;
    private int currentCate = item1;
    private int clickCate = item1;

    //viewPager滑动页面
    private List<View> listViews = null;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_phone_share);
    }

    @Override
    public void initData() {
        super.initData();

        init();

        adapter = new PhoneShareListAdapter(aty);
        adapter.setList(products);

        listViews = new ArrayList<View>();
        LayoutInflater mInflater = getLayoutInflater();
        listViews.add(mInflater.inflate(R.layout.item_viewpager, null));
        listViews.add(mInflater.inflate(R.layout.item_viewpager, null));

        pageSwitch();
    }

    private void init() {
        Product item = new Product();
        item.setPrice(BigDecimal.valueOf(11));
        item.setDisparity(5);
        item.setName("酒");
        products.add(item);

        Product item2 = new Product();
        item2.setPrice(BigDecimal.valueOf(22));
        item.setDisparity(6);
        item2.setName("酒2");
        products.add(item2);

        Product item3 = new Product();
        item3.setPrice(BigDecimal.valueOf(33));
        item3.setName("酒3");
        item.setDisparity(7);
        products.add(item3);

        Product item4 = new Product();
        item4.setPrice(BigDecimal.valueOf(44));
        item4.setName("酒4");
        item.setDisparity(8);
        products.add(item4);
    }

    @Override
    public void initWidget() {
        super.initWidget();

        // 分类下的彩色条
        gestureDetector = new GestureDetector(this); // 手势滑动
        windowsWidth = this.getWindowManager().getDefaultDisplay().getWidth();
        int offset = ListUtils.dip2px(aty, 20);
        slidebar.getLayoutParams().width = windowsWidth/2-offset;

        viewPager = (ViewPager) this.findViewById(R.id.viewPager);
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(new MyPagerAdapter(listViews));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int state) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int index) {
                clickCate = index;
                moveAnimation(currentCate - clickCate);
                currentCate = index;
                pageSwitch();
            }
        });

        if ( commoditiesList != null ) {
            commoditiesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
                    showActivity(aty,DetailActivity.class);
                }
            });
        }
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.phoneshare_back:
                finish();
                break;
        }
    }

    //不同页面切换
    private void pageSwitch() {
        initListData(currentCate);
        viewPager.setCurrentItem(currentCate, true);
    }

    //不同页面初始化数据
    private void initListData(int index) {
        commoditiesList = (ListView) listViews.get(index).findViewById(R.id.phoneshare_commodities);
        commoditiesList.setAdapter(adapter);
    }

    //分类底部彩色条移动
    private void moveAnimation(final int num) {
        final int offset = ListUtils.dip2px(aty, 20);
        TranslateAnimation animation = new TranslateAnimation(0,
                -(windowsWidth/2-offset)*num, 0, 0);
        animation.setDuration(200);
        slidebar.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                slidebar.clearAnimation();
                slidebar.setX(slidebar.getX() - (windowsWidth / 2 - offset) * num);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationStart(Animation arg0) {
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }
}
